unit Class_DATA;

interface
uses
  Classes,SysUtils,DB,Uni,UniEngine;

type
  TDATA=class(TUniEngine)
  private
    FDATACODE : string;
    FDATATYPE : Integer;
    FDATAVAL  : Extended;
  protected
    procedure SetParameters;override;
    function  GetStrInsert:string;override;
    function  GetStrUpdate:string;override;
    function  GetStrDelete:string;override;
  public
    function  GetStrsIndex:string;override;
  public
    function  GetNextIndx:Integer;overload;
    function  GetNextIndx(AUniConnection:TUniConnection):Integer;overload;
  public
    function  CheckExist(AUniConnection:TUniConnection):Boolean;override;
  public
    destructor  Destroy; override;
    constructor Create;  
  published
    property DATACODE : string read FDATACODE  write FDATACODE;
    property DATATYPE : Integer read FDATATYPE  write FDATATYPE;
    property DATAVAL  : Extended read FDATAVAL   write FDATAVAL;    
  public
    class function  ReadDS(AUniQuery:TUniQuery):TUniEngine;override;
    class procedure ReadDS(AUniQuery:TUniQuery;var Result:TUniEngine);override;
    class function  CopyIt(ADATA:TDATA):TDATA;overload;        
    class procedure CopyIt(ADATA:TDATA;var Result:TDATA);overload;       
  end;

implementation

{ TDATA }
procedure TDATA.SetParameters;
begin
  inherited;
  with FUniSQL.Params do
  begin
    case FOptTyp of
      otAddx:
      begin
        ParamByName('DATA_CODE').Value := DATACODE;
        ParamByName('DATA_TYPE').Value := DATATYPE;
        ParamByName('DATA_VAL').Value := DATAVAL; 
      end;
      otEdit:
      begin
        ParamByName('DATA_CODE').Value := DATACODE;
        ParamByName('DATA_TYPE').Value := DATATYPE;
        ParamByName('DATA_VAL').Value := DATAVAL; 
      end;  
      otDelt:
      begin
        ParamByName('DATA_CODE').Value := DATACODE;
 
      end;  
    end;
  end;
end;

function TDATA.CheckExist(AUniConnection: TUniConnection): Boolean;
begin
  Result:=CheckExist('TBL_DATA',['DATA_CODE',DATACODE],AUniConnection);
end;

function TDATA.GetNextIndx: Integer;
begin

end;

function TDATA.GetNextIndx(AUniConnection: TUniConnection): Integer;
begin

end;

function TDATA.GetStrDelete: string;
begin
  Result:='DELETE FROM TBL_DATA WHERE   DATA_CODE=:DATA_CODE';
end;

function TDATA.GetStrInsert: string;
begin
  Result:='INSERT INTO TBL_DATA'
         +'    ( DATA_CODE, DATA_TYPE, DATA_VAL)'
         +'    VALUES'
         +'    (:DATA_CODE,:DATA_TYPE,:DATA_VAL)';
end;

function TDATA.GetStrsIndex: string;
begin
  Result:=Format('%S',[DATACODE]);
end;

function TDATA.GetStrUpdate: string;
begin
  Result:='UPDATE  TBL_DATA SET'
         +'    DATA_TYPE=:DATA_TYPE,'
         +'    DATA_VAL=:DATA_VAL'
         +'    WHERE DATA_CODE=:DATA_CODE';
end;

constructor TDATA.Create;
begin

end;

destructor TDATA.Destroy;
begin

  inherited;
end;

class function TDATA.ReadDS(AUniQuery: TUniQuery): TUniEngine;
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  Result:=TDATA.Create;

  with TDATA(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=UpperCase(Field.FieldName);
      if FieldName='DATA_CODE' then
      begin
        DATACODE :=Field.AsString;
      end else
      if FieldName='DATA_TYPE' then
      begin
        DATATYPE :=Field.AsInteger;
      end else
      if FieldName='DATA_VAL' then
      begin
        DATAVAL  :=Field.AsFloat;
      end;
    end
  end;  
end;

class procedure TDATA.ReadDS(AUniQuery: TUniQuery; var Result: TUniEngine);
var
  I:Integer;
  Field:TField;
  FieldName:string;
begin
  if Result=nil then Exit;

  with TDATA(Result) do
  begin
    for I:=0 to AUniQuery.Fields.Count-1 do
    begin
      Field:=AUniQuery.Fields.Fields[I];
      //if field.fieldname is not all uppercase,please use uppercase().
      FieldName:=UpperCase(Field.FieldName);
      if FieldName='DATA_CODE' then
      begin
        DATACODE :=Field.AsString;
      end else
      if FieldName='DATA_TYPE' then
      begin
        DATATYPE :=Field.AsInteger;
      end else
      if FieldName='DATA_VAL' then
      begin
        DATAVAL  :=Field.AsFloat;
      end;
    end
  end;  
end;

class function  TDATA.CopyIt(ADATA: TDATA): TDATA;
begin
  Result:=TDATA.Create;
  TDATA.CopyIt(ADATA,Result)
end;

class procedure TDATA.CopyIt(ADATA:TDATA;var Result:TDATA);
begin
  if Result=nil then Exit;
  Result.DATACODE :=ADATA.DATACODE;
  Result.DATATYPE :=ADATA.DATATYPE;
  Result.DATAVAL  :=ADATA.DATAVAL;
end;

end.

